package springboot.test15.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

//过时技术@Deprecated
//@Configuration
public class DataSourceConfig {
    //默认的自动配置是判断容器中没有才会配@ConditionalOnMissingBean(DataSource.class)
    //配置druid数据源
    @Bean
    //与spring.datasource进行绑定
    @ConfigurationProperties("spring.datasource")
    public DataSource datasource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
/*
        druidDataSource.setUrl();
        druidDataSource.setUsername();
        druidDataSource.setPassword();*/

        //加入监控 防火墙 也可以使用application.yaml来配置
        //druidDataSource.setFilters("stat,wall");
        return druidDataSource;
    }

    //配置druid监控页功能 输入目标网址/druid即可
    @Bean
    public ServletRegistrationBean statViewServlet(){
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(statViewServlet,"/druid/*");
        registrationBean.addInitParameter("loginUsername","root");
        registrationBean.addInitParameter("loginPassword","123");
        return registrationBean ;
    }
    //内置监控中的Web和Spring关联监控怎么配置
    //WebStatFilter 用于采集web-jdbc管理监控数据
    @Bean
    public FilterRegistrationBean webStatFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>(webStatFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}
